package com.test2;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class test2 {
    public static void main(String[] args) {
        int titleSize = 128;
        //必须加入的字符串
        String[] mustString = new String[]{"Car Radio","Multimedia","Android 11"};
        //选择的字符串
        String[] chooseString = new String[]{"Wireless Carplay Auto","Player","Navigation GPS",
        "Video","No 2din","Autoradio","Stereo","SWC","4G","WIFI","BT","DSP","RDS","ADAS","AHD"};
        Random r = new Random();
        //导出的汽车数据
        List<Car> carOut = new ArrayList<>();
        Car carTitle = new Car();
        carTitle.setNumber("编码");
        carTitle.setName("名称");
        carTitle.setYear("年份");
        carTitle.setQuality("重量");
        carTitle.setVolume("体积");
        carTitle.setLength("长");
        carTitle.setWide("宽");
        carTitle.setHigh("高");
        carTitle.setSize("尺寸");
        carTitle.setNote("备注");
        carTitle.setPrice("框价格");
        carTitle.setTitle("标题");
        carTitle.setTitleSize("标题长度");
        carOut.add(carTitle);
        //读取进来的汽车数据
        List<Car> cars = excleIn();
        //9inch的个数
        int nineInchSize = 0;
        for (Car car:cars) {
            //标题字符串
            StringBuffer title = new StringBuffer();
            //如果屏幕大小为10.1,则将尺寸加进去,如果屏幕大小为9，则只加部分的进去
            if(car.getSize().equals("10.1")){
                title.append("10.1 Inch");
            }else {
                if(car.getSize().equals("9")){
                    ++nineInchSize;
                    //3，6，9，12，15等就加入
                    if(nineInchSize%3 == 0){
                        title.append("9 Inch");
                    }
                }
            }
            title.append(" ");
            title.append(mustString[2]);
            title.append(" ");
            title.append("For");
            title.append(" ");
            title.append(car.getName());
            title.append(" ");
            if(!car.getYear().equals("null")){
                title.append(car.getYear());
                title.append(" ");
            }
            title.append(mustString[0]);
            //剩余的字符串长度，用于组合Multimedia加选择的字符串
            int remainingSize = titleSize-(title.toString().length())-mustString[1].length();
            //对数组进行随机排序
            shuffle(chooseString);
            System.out.println(Arrays.toString(chooseString));
            StringBuffer remainingString = new StringBuffer();
            int remainingIndex = 0;
            for (int i=0; i<chooseString.length; i++){
                remainingString.append(" ");
                remainingString.append(chooseString[i]);
                if(remainingString.toString().length()+1>remainingSize){
                    remainingIndex = i;
                    break;
                }
            }
            int mIndex = r.nextInt(remainingIndex);
            for (int i=0; i<remainingIndex; i++){
                title.append(" ");
                title.append(chooseString[i]);
                if(i == mIndex){
                    title.append(" ");
                    title.append(mustString[1]);
                }
            }
            System.out.println(title.toString());
            while (true){
                if(remainingIndex >= chooseString.length){
                    break;
                }
                if(chooseString[remainingIndex].length()+title.toString().length()>127){
                    remainingIndex++;
                    continue;
                }else {
                    title.append(" ");
                    title.append(chooseString[remainingIndex]);
                }
                remainingIndex++;
            }
            car.setTitle(title.toString().trim());
            car.setTitleSize(String.valueOf(title.toString().trim().length()));
            carOut.add(car);
            System.out.println(title.toString().trim());
            System.out.println(title.toString().trim().length());
        }
        excleOut(carOut);
    }
    /**
     * 针对Car类进行导出的操作
     * @param list
     */
    public static void excleOut(List<Car> list) {
        WritableWorkbook car = null;
        try {
            // 创建一个excle对象
            car = Workbook.createWorkbook(new File("D:/商品标题/Fecheiws Navi Store.xls"));
            // 通过excle对象创建一个选项卡对象
            WritableSheet sheet = car.createSheet("sheet1", 0);
            // 创建一个单元格对象 列 行 值
            // Label label = new Label(0, 2, "test");
            for (int i = 0; i < list.size(); i++) {
                Car car2 = list.get(i);
                //编码
                Label label1 = new Label(0, i, car2.getNumber());
                //名称
                Label label2 = new Label(1, i, car2.getName());
                //年份
                Label label3 = new Label(2, i, car2.getYear());
                //重量
                Label label4 = new Label(3, i, car2.getQuality());
                //体积
                Label label5 = new Label(4, i, car2.getVolume());
                //长
                Label label6 = new Label(5, i, car2.getLength());
                //宽
                Label label7 = new Label(6, i, car2.getWide());
                //高
                Label label8 = new Label(7, i, car2.getHigh());
                //尺寸
                Label label9 = new Label(8, i, car2.getSize());
                //备注
                Label label10 = new Label(9, i, car2.getNote());
                //框价格
                Label label11 = new Label(10, i, car2.getPrice());
                //标题
                Label label12 = new Label(11, i, car2.getTitle());
                //标题长度
                Label label13 = new Label(12, i, car2.getTitleSize());

                // 将创建好的单元格对象放入选项卡中
                sheet.addCell(label1);
                sheet.addCell(label2);
                sheet.addCell(label3);
                sheet.addCell(label4);
                sheet.addCell(label5);
                sheet.addCell(label6);
                sheet.addCell(label7);
                sheet.addCell(label8);
                sheet.addCell(label9);
                sheet.addCell(label10);
                sheet.addCell(label11);
                sheet.addCell(label12);
                sheet.addCell(label13);
            }
            // 写入目标路径
            car.write();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                car.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 针对Car类进行导入的操作
     * @return
     */
    public static List<Car> excleIn() {
        List<Car> list = new ArrayList<>();
        Workbook workbook = null;
        try {
            // 获取Ecle对象
            workbook = Workbook.getWorkbook(new File("D:/商品标题/car_template1.xls"));
            // 获取选项卡对象 第0个选项卡
            Sheet sheet;
            sheet = workbook.getSheet(0);
            // 循环选项卡中的值
            for (int i = 1; i < sheet.getRows(); i++) {
                Car car = new Car();
                //编码
                car.setNumber(sheet.getCell(0, i).getContents());
                //名称
                car.setName(sheet.getCell(1, i).getContents());
                //年份
                car.setYear(sheet.getCell(2, i).getContents());
                //重量
                car.setQuality(sheet.getCell(3, i).getContents());
                //体积
                car.setVolume(sheet.getCell(4, i).getContents());
                //长
                car.setLength(sheet.getCell(5, i).getContents());
                //宽
                car.setWide(sheet.getCell(6, i).getContents());
                //高
                car.setHigh(sheet.getCell(7, i).getContents());
                //尺寸
                car.setSize(sheet.getCell(8, i).getContents());
                //备注
                car.setNote(sheet.getCell(9, i).getContents());
                //框价格
                car.setPrice(sheet.getCell(10, i).getContents());
                list.add(car);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workbook.close();
        }
        return list;
    }

    /**
     * 对数组进行随机排序
     * @return
     */
    private static Random rand = new Random();

    public static <T> void swap(T[] a, int i, int j) {
        T temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static <T> void shuffle(T[] arr) {
        int length = arr.length;
        for (int i = length; i > 0; i--) {
            int randInd = rand.nextInt(i);
            swap(arr, randInd, i - 1);
        }
    }
}
